package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.domain.Comments;
import com.example.blog.domain.UserInformation;
import com.example.blog.domain.Writings;
import com.example.blog.dto.comments.AdminSelectDto;
import com.example.blog.mapper.CommentsMapper;
import com.example.blog.service.CommentsService;
import com.example.blog.service.UserInformationService;
import com.example.blog.service.WritingService;
import com.example.blog.vo.comments.AdminCommentsSelectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    @Resource
    private UserInformationService userInformationService;
    @Resource
    private WritingService writingsService;
    @Resource
    private CommentsMapper commentsMapper;

    //Admin查询评论
    @Override
    public List<Object> AdminSelectComment(AdminSelectDto comments) {
        IPage<Comments> page = new Page<>(comments.getIndex(), comments.getPage());
        LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Comments::getDateTime)
                .like(Objects.nonNull(comments.getDateTime()), Comments::getDateTime, comments.getDateTime())
                .eq(Objects.nonNull(comments.getWid()), Comments::getWid, comments.getWid())
                .eq(Objects.nonNull(comments.getUid()), Comments::getUid, comments.getUid());
        List<Comments> comments_ = commentsMapper.selectPage(page, wrapper).getRecords();
        List<Object> result = new ArrayList<>();
        //如果查询不到数据直接返回空
        if(comments_.size()<1){
            result.add(null);
            result.add(page);
            return result;
        }

        List<AdminCommentsSelectVo> collect = comments_.stream().map(e -> new AdminCommentsSelectVo(
                e.getCid(), e.getContent(), e.getDateTime(), e.getStatue(), e.getUid(), e.getWid()
        )).collect(Collectors.toList());
        List<String> wid = comments_.stream().map(Comments::getWid).collect(Collectors.toList());
        List<Long> uid = comments_.stream().map(Comments::getUid).collect(Collectors.toList());

        LambdaQueryWrapper<Writings> writingsWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UserInformation> userInfoWrapper = new LambdaQueryWrapper<>();
        List<Writings> writingsList = writingsService.list(
                writingsWrapper.in(Writings::getWid, wid).select(Writings::getWid, Writings::getTitle));
        List<UserInformation> userInformationList = userInformationService.list(
                userInfoWrapper.in(UserInformation::getUid, uid).select(UserInformation::getUid, UserInformation::getUsername));
        collect.forEach(s -> {
            writingsList.forEach(ws -> {
                if (Objects.equals(ws.getWid(), s.getWid())) {
                    s.setTitle(ws.getTitle());
                }
            });
            userInformationList.forEach(us -> {
                if (Objects.equals(us.getUid(), s.getUid())) {
                    s.setUsername(us.getUsername());
                }
            });
        });

        result.add(collect);
        result.add(page);
        return result;
    }
}


